package com.soushangxing.comfyuibackendv2.service

import com.soushangxing.comfyuibackendv2.bean.ResponseType
import com.soushangxing.comfyuibackendv2.entity.ModelEntity
import com.soushangxing.comfyuibackendv2.repository.ModelRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*

@Service
class ModelService {

    @Autowired
    lateinit var modelRepository: ModelRepository

    // 添加新模型
    fun add(name: String, typeCode: String, description: String, prompt: String, status: String): ModelEntity {
        val model = ModelEntity().apply {
            this.name = name
            this.typeCode = typeCode
            this.description = description
            this.prompt = prompt
            this.status = status
        }
        return modelRepository.save(model)
    }

    // 更新现有模型
    fun update(name: String, typeCode: String, description: String, prompt: String, status: String): Optional<ModelEntity> {
        return modelRepository.findByName(name).map { existingModel ->
            existingModel.apply {
                this.typeCode = typeCode
                this.description = description
                this.prompt = prompt
                this.status = status
            }
            modelRepository.save(existingModel)
        }
    }

    // 根据名称删除模型
    fun delete(name: String): Boolean {
        val model = modelRepository.findByName(name)
        return if (model.isPresent) {
            modelRepository.delete(model.get())
            true
        } else {
            false
        }
    }

    fun findByStatus(status: String): List<ModelEntity> {
        return modelRepository.findAllByStatus(status)
    }
}
